$('document').ready(function () {
    // 加载页面时触发的事件
    if (parseInt(getCookie('code')) !== 203) {
        window.location.href = "home.html"
    }
    // name-input框事件
    $('#name-input').blur(function () {
        if ($('#name-input').val() == '') {
            $('#name-input').css('border', '2px solid red')
            $('#name-input').prop('placeholder', '姓名不能为空!')
        } else if ($('#name-input').val().length > 12) {
            $('#name-input').prop('placeholder', '长度不能大于12位!')
            $('#name-input').val('')
            $('#name-input').css('border', '2px solid red')
        } else {
            $('#name-input').css('border', '2px solid green')
        }
    })
    $('#name-input').keyup(function () {
        if (($('#name-input').val() !== '')) {
            $('#name-input').css('border', '2px solid green')
        } else {
            $('#name-input').css('border', '2px solid red')
            $('#name-input').prop('placeholder', '姓名不能为空!')
        }

    })
    // password-input框事件
    $('#password-input').blur(function () {
        if ($('#password-input').val() == '') {
            $('#password-input').css({
                'border': '2px solid red',
            })
            $('#password-input').prop('placeholder', '密码不能为空!')
            $('#checkpsw').css('cursor', 'auto')
        } else if ($('#password-input').val().length < 9 && $('#password-input').val().length > 0) {
            $('#password-input').css('border', '2px solid green')
            $('#checkpsw').css('cursor', 'pointer')
        }
    })
    $('#password-input').keyup(function () {
        if ($('#password-input').val().length < 9 && $('#password-input').val().length > 0) {
            $('#checkpsw').css('cursor', 'pointer')
            $('#password-input').css('border', '2px solid green')
        } else {
            $('#checkpsw').css('cursor', 'auto')
            $('#password-input').prop('placeholder', '密码不能为空!')
            $('#password-input').css('border', '2px solid red')
        }

    })
    // 查看密码
    $('#checkpsw').click(function () {
        if ($('#password-input').attr('type') == 'password') {
            $('#password-input').attr('type', 'text')
        } else if ($('#password-input').attr('type') == 'text') {
            $('#password-input').attr('type', 'password')
        }
    })
    // email-input框事件
    $('#email-input').blur(function () {
        if ($('#email-input').val() == '') {
            $('#email-input').css({
                'border': '2px solid red',
            })
            $('#email-input').prop('placeholder', '账号不能为空!')
        } else {
            $('#email-input').css('border', '2px solid green')
        }
    })
    $('#email-input').keyup(function () {
        if ($('#email-input').val() == '') {
            $('#email-input').prop('placeholder', '账号不能为空!')
            $('#email-input').css('border', '2px solid red')
        } else {
            $('#email-input').css('border', '2px solid green')
        }
    })
    // company-input框事件
    $('#company-input').blur(function () {
        if ($('#company-input').val() == '') {
            $('#company-input').css({
                'border': '2px solid red',
            })
            $('#company-input').prop('placeholder', '职称不能为空!')
        } else if ($('#company-input').val().length > 0) {
            $('#company-input').css('border', '2px solid green')
        }
    })
    $('#company-input').keyup(function () {
        if ($('#company-input').val().length > 0) {
            $('#company-input').css('border', '2px solid green')
        } else {
            $('#company-input').prop('placeholder', '职称不能为空!')
            $('#company-input').css('border', '2px solid red')
        }

    })
    // -------------------------------------------------------------
    // 注册
    $('#register').click(function () {
        if (($('#name-input').val() !== '') && $('#password-input').val().length < 9 && $('#password-input').val().length > 0 && $('#email-input').val().length > 0 && $('#company-input').val().length > 0) {
            $.ajax({
                type: 'POST',
                url: 'http://111.229.120.67/sch/add-teac',
                data: {
                    "id": $('#email-input').val(),
                    "realname": $('#name-input').val(),
                    "password": $('#password-input').val(),
                    "profession": $('#company-input').val()
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $('#name-input').val('')
                        $('#password-input').val('')
                        $('#email-input').val('')
                        $('#company-input').val('')

                        $('#myModal-public h4').html('添加成功')
                        $('#myModal-public-body').html('成功添加教师用户')
                        $('#myModal-public').modal({
                            keyboard: true,
                        })
                    } else {
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })

        } else if ($('#email-input').val() == '') {
            $('#email-input').prop('placeholder', '账号不能为空!')
            $('#email-input').css('border', '2px solid red')
        } else if ($('#password-input').val() == '' || $('#password-input').val().length > 9) {
            $('#password-input').prop('placeholder', '密码格式错误!')
            $('#password-input').css('border', '2px solid red')
        } else if ($('#company-input').val() == '') {
            $('#company-input').prop('placeholder', '职称不能为空!')
            $('#company-input').css('border', '2px solid red')
        } else if ($('#name-input').val() == '') {
            $('#name-input').prop('placeholder', '姓名不能为空!')
            $('#name-input').css('border', '2px solid red')
        }
    })
    // --------------------------
})